package org.jeecg.modules.satff.controller;

import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liquibase.pro.packaged.S;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.satff.entity.Satff;
import org.jeecg.modules.satff.service.ISatffService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 人事表
 * @Author: nbacheng
 * @Date:   2024-09-29
 * @Version: V1.0
 */
@Api(tags="人事表")
@RestController
@RequestMapping("/satff/satff")
@Slf4j
public class SatffController extends JeecgController<Satff, ISatffService> {
	@Autowired
	private ISatffService satffService;

	/**
	 * 分页列表查询
	 *
	 * @param satff
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "人事表-分页列表查询")
	@ApiOperation(value="人事表-分页列表查询", notes="人事表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Satff satff,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Satff> queryWrapper = QueryGenerator.initQueryWrapper(satff, req.getParameterMap());
		Page<Satff> page = new Page<Satff>(pageNo, pageSize);
		IPage<Satff> pageList = satffService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param satff
	 * @return
	 */
	@AutoLog(value = "人事表-添加")
	@ApiOperation(value="人事表-添加", notes="人事表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Satff satff) {
		satffService.save(satff);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param satff
	 * @return
	 */
	@AutoLog(value = "人事表-编辑")
	@ApiOperation(value="人事表-编辑", notes="人事表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Satff satff) {
		//实际转正日期已过，则设置为已转正
		if (satff.getRealFormalTime() != null&&satff.getRealFormalTime().before(new Date())) {
			satff.setEntryStatus(2);
		}
		satffService.updateById(satff);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "人事表-通过id删除")
	@ApiOperation(value="人事表-通过id删除", notes="人事表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		satffService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "人事表-批量删除")
	@ApiOperation(value="人事表-批量删除", notes="人事表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.satffService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "人事表-通过id查询")
	@ApiOperation(value="人事表-通过id查询", notes="人事表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Satff satff = satffService.getById(id);
		if(satff==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(satff);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param satff
    */
    @RequestMapping(value = "/exportEntryXls")
    public ModelAndView exportEntryXls(HttpServletRequest request, Satff satff) {
		String exportFields = "staffName,entryTime,staffJob,staffDepartmentIds,staffType,entryStatus";
        return super.exportXls(request, satff, Satff.class, "已入职导出成员信息",exportFields);
    }

	 /**
	  * 导出excel
	  * @param request
	  * @param satff
	  */
	 @RequestMapping(value = "/exportFormalXls")
	 public ModelAndView exportFormalXls(HttpServletRequest request, Satff satff) {
		 String exportFields = "staffName,entryTime,staffJob,staffDepartmentIds,realFormalTime";
		 return super.exportXls(request, satff, Satff.class, "已转正导出成员信息",exportFields);
	 }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Satff.class);
    }

}
